<template>
  <div class="echarts" ref="echarts"></div>
</template>

<script>
import '../../../public/static/theme/chalk.js'

export default {
  name: "Rank",
  data() {
    return {
      chartInstance: null,
      data: null,
      startValue: 0, // 区域缩放的起点值
      endValue: 9, // 区域缩放的终点值
      timerId: null
    }
  },
  mounted() {
    this.initChartInstance()
    this.getData()
    window.addEventListener("resize", this.screenAdapter)
    this.screenAdapter()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    initChartInstance() {
      this.chartInstance = this.$echarts.init(this.$refs.echarts, 'chalk')
      const initOption = {
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: {
          type: 'bar'
        },
        title: {
          text: '😀 地区销售排行',
          left: 20,
          top: 20
        },
        grid: {
          top: '40%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {},
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData() {
      // 获取服务器的数据，对this.data 进行赋值之后，调用updateChart方法更新图表
      const {data: res} = await this.$http.get('rank')
      this.data = res
      this.data.sort((a, b) => {
        return b.value - a.value
      })
      // console.log(this.data);
      this.updateChart()
      this.startInterval()
    },
    updateChart() {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      // 处理图表需要的数据
      const provinceArr = this.data.map(item => item.name)
      const valueArr = this.data.map(item => item.value)
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        // 数据的区域缩放
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: [{
          data: valueArr,
          itemStyle: {
            color: arg => {
              let targetColorArr = null
              if (arg.value > 300) {
                targetColorArr = colorArr[0]
              } else if (arg.value > 200) {
                targetColorArr = colorArr[1]
              } else {
                targetColorArr = colorArr[2]
              }
              return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: targetColorArr[0]
                },
                {
                  offset: 1,
                  color: targetColorArr[1]
                }
              ])
            }
          }
        }],
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter() {
      // 自适应分辨率
      const titleFontSize = this.$refs.echarts.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              borderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()

    },
    // 区域缩放函数
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        if (this.endValue > this.data.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.startValue++
        this.endValue++
        this.updateChart()
      }, 2000)
    }
  }
}
</script>

<style scoped>
.echarts {
  height: 800px;
  width: 100%;
}

</style>
